﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Bainx.WPF.Extend.Control
{
    /// <summary>
    /// 对TabControl控件的扩展，只能通过设置SelectedIndex或当IsMouseOver=true时才会处理SelectionChanged事件
    /// </summary>
    public class Tabs : TabControl
    {
       
        private bool AllowChangeSelectedIndex = true;
        private int LastIndex;

        private int currentIndex = -1;
        private int lastIndex = -1;

        public int CurrentIndex
        {
            get { return currentIndex; }
            set
            {
                AllowChangeSelectedIndex = true;
                base.SelectedIndex = value;
            }
        }

        //public new object SelectedItem 
        //{
        //    get { return base.SelectedItem; }
        //    set
        //    { 
        //        base.
        //    }
        //}

        protected override void OnSelectionChanged(SelectionChangedEventArgs e)
        {
            System.Console.WriteLine("$$$$$$$$$$$$$$$$(this.IsMouseOver || AllowChangeSelectedIndex) = {0}", this.IsMouseOver || AllowChangeSelectedIndex);
            System.Console.WriteLine("$$$$$$$$$$$$$$$$this.SelectedIndex={0}",this.SelectedIndex);
            if (this.IsMouseOver || AllowChangeSelectedIndex)
            {
                base.OnSelectionChanged(e);               
                AllowChangeSelectedIndex = false;
                lastIndex = currentIndex;
                currentIndex = this.SelectedIndex;
            }
            else
            {
                this.SelectedItem = e.RemovedItems[0];
            }
        }
    }
}
